package cn.dyp.com.agriculture.dialog;

import android.app.Activity;
import android.app.DialogFragment;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.dyp.com.agriculture.ClientApp;
import cn.dyp.com.agriculture.R;

/**
 * Created by lenovo on 2017/5/7.
 */

public class IPSetDialog extends DialogFragment{
    private EditText mIp1,mIp2,mIp3,mIp4;
    private Button mCloseBtn,mOkBtn;
    private ClientApp mApp;
    private Activity mActivity;

    public IPSetDialog(Activity activity) {
        this.mApp = (ClientApp) activity.getApplication();
        //mApp.setActivity(activity);
        this.mActivity = activity;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        return inflater.inflate(R.layout.ip_dialog, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        setListener();
        setValue();
    }


    //显示Ip地址
    private void setValue() {
        String ipStr[] = mApp.getServerIpStr().split("\\.");
        if (ipStr.length >= 4) {
            mIp1.setText(ipStr[0]);
            mIp2.setText(ipStr[1]);
            mIp3.setText(ipStr[2]);
            mIp4.setText(ipStr[3]);
        }
    }


    //初始化控件
    private void initView() {
        mIp1 = (EditText) getView().findViewById(R.id.ip1_et);
        mIp2 = (EditText) getView().findViewById(R.id.ip2_et);
        mIp3 = (EditText) getView().findViewById(R.id.ip3_et);
        mIp4 = (EditText) getView().findViewById(R.id.ip4_et);
        mCloseBtn = (Button) getView().findViewById(R.id.close);
        mOkBtn = (Button) getView().findViewById(R.id.ok_button);
    }


    //用匿名类的方式注册监听器
    private void setListener() {
        mCloseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IPSetDialog.this.dismiss();
            }
        });
        mOkBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                setIP();
                IPSetDialog.this.dismiss();
            }
        });
    }

    //对用户输入的Ip进行转化、校验和保存
    private void setIP() {
        try {
            // 获取Ip地址
            int ip1, ip2, ip3, ip4;
            ip1 = Integer.parseInt(mIp1.getText().toString().trim());
            ip2 = Integer.parseInt(mIp2.getText().toString().trim());
            ip3 = Integer.parseInt(mIp3.getText().toString().trim());
            ip4 = Integer.parseInt(mIp4.getText().toString().trim());

            // 校验Ip地址
            String ipStr = ip1 + "." + ip2 + "." + ip3 + "." + ip4;
            if (null == ipStr || "".equals(ipStr)) {
                showAlertDialog(getString(R.string.prompt), getString(R.string.invalid_ip));
                return;
            } else {
                //用正则表达式来对字符串进行匹配，将给定的正则表达式编译并赋予给Pattern类
                Pattern p = Pattern.compile("(2[5][0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})");
                Matcher m = p.matcher(ipStr);
                if (!m.matches()) {
                    showAlertDialog(getString(R.string.prompt), getString(R.string.invalid_ip));
                    return;
                }
            }
            mApp.setServerIpStr(ipStr);
        } catch (Exception e) {
            showAlertDialog(getString(R.string.prompt), getString(R.string.invalid_ip));
        }
    }

    private void showAlertDialog(String title, String message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setPositiveButton(mActivity.getString(android.R.string.ok), null);
        builder.show();
    }
}
